package org.feng.config;

import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingModel;
import io.qdrant.client.QdrantClient;
import lombok.SneakyThrows;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.qdrant.QdrantVectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 聊天配置
 *
 * @author pine
 * @version v1.0
 * @since 2025-08-12 20:22
 */
@Configuration
public class ChatConfig {


    /**
     * 配置ChatClient，注册系统指令和工具函数
     */
    @Bean
    @SneakyThrows
    public ChatClient chatClient(ChatClient.Builder builder) {
        return builder.defaultSystem("""
                                 你是一个资深的java专家，请在开发中遵循如下规则：
                                 - 严格遵循 SOLID、DRY、KISS、YAGNI 原则
                                 - 遵循 OWASP 安全最佳实践（如输入验证、SQL注入防护）
                                 - 采用 分层架构设计，确保职责分离
                                 - 代码变更需通过 单元测试覆盖（测试覆盖率 ≥ 80%）
                        """)
                .build();
    }

    /**
     * 内存聊天记忆
     */
    @Bean
    public MessageWindowChatMemory chatMemory() {
        return MessageWindowChatMemory.builder().maxMessages(100).build();
    }


    /**
     * 向量数据库 qdrant
     */
    @Bean
    public VectorStore vectorStore(@Autowired QdrantClient qdrantClient, @Autowired DashScopeEmbeddingModel dashScopeEmbeddingModel) {
        return QdrantVectorStore.builder(qdrantClient, dashScopeEmbeddingModel).build();
    }
}
